#include "chess/chessboard.h"
#include "chess/chesspane.h"
#include <iostream>
#include <stdio.h>

using namespace std;

// 下一步棋
bool next(ChessBoard* b, const char str[], int x = -1, int y = -1)
{
    //getchar();
    if (x == -1)
        b->getBestPlace(x, y);
    cout << str << ": (" << x << "," << y << ")" << endl;
    b->saveChess(x, y);
    b->showBoard();
    return b->scanBoard();
}

// 全自动下棋（电脑相互对抗）
int a()
{
    ChessBoard b;
    while (true) {
        if (next(&b, "BLACK") == true)
            break;
        if (next(&b, "WHITE") == true)
            break;
    }
    cout << b.lastPane->color << " win." << endl;
    return 0;
}

// 手动下棋
int m(){
    int N =0;
    // 获取棋盘大小
    while( N < 5){
        cout << " >> Board(N x N), N = ?";
        cin >> N;
    }
    ChessBoard* b = new ChessBoard(N);
    // 游戏开始
    int x = 0, y = 0;
    b->showBoard();
    cout << "--\nGoBang Game Start!!\n" << "(-1,-1) means UNDO !\n--\n";
    while (true) {
        // 获取用户的下子信息
        cout << "BLACK (x,y) = ";
        cin >> x >> y;
        while (x < 0 || y < 0|| x >= N || y >= N){
            b->undo();
            b->undo();
            b->showBoard();
            cout << "Undo!! new (x,y) = ";
            cin >> x >> y;
        }
        b->saveChess(x, y);
        if (b->scanBoard()) break;

        // 获取电脑的下子信息
        b->getBestPlace(x, y);
        b->saveChess(x, y);
        b->showBoard();
        cout << "WHITE : (" << x << "," << y << ")" << endl;
        if (b->scanBoard()) break;
    }
    cout << b->lastPane->color << " win." << endl;
    return 0;
}

int main()
{
    return m();
    //return a();
}
